package com.hmall.controller.back;

import com.google.gson.Gson;
import com.hdmall.business.common.annotation.AuthRequired;
import com.hdmall.business.common.annotation.LoginRequired;
import com.hdmall.business.common.common.ServerResponse;

import com.hdmall.business.common.util.HttpClientUtil;
import com.hmall.mapper.*;

import com.hmall.model.*;

import com.hmall.util.TokenUtil;

import com.hmall.vo.*;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.springframework.beans.factory.annotation.Autowired;


import org.springframework.web.bind.annotation.*;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.io.*;

import java.math.BigDecimal;

import java.text.SimpleDateFormat;
import java.util.*;


@RestController
@RequestMapping("/manage/bi")
@Api(value = "bi管理操作API", description = "bi管理类API说明")
public class BIManageController {
    private final
    MallProductPlanItemMapper mallProductPlanItemMapper;
    private final
    MallOrderMapper mallOrderMapper;
    private final
    MallDealerMapper mallDealerMapper;
    private final
    SysDeptMapper sysDeptMapper;
    private final
    MallPaymentVoucherMapper mallPaymentVoucherMapper;
    private final
    BiMonthDealerMapper biMonthDealerMapper;
    private final
    MallOrderItemMapper mallOrderItemMapper;
    private final
    SysUserMapper sysUserMapper;
    private final
    BiMonthUserMapper biMonthUserMapper;
    private final
    MallProductMapper mallProductMapper;
    private final
    BiMonthProductMapper biMonthProductMapper;
    private final
    MallAttrConnectMapper mallAttrConnectMapper;
    private final
    JedisPool jedisPool;
    private final
    SysCustomerMapper sysCustomerMapper;
    private final
    SysQuestionMapper sysQuestionMapper;
    private final
    SysQuestionAnswerMapper sysQuestionAnswerMapper;
    private final
    SysQuestionAchievementMapper sysQuestionAchievementMapper;
    private final
    DictionMapper dictionMapper;

    /*@Autowired
    RedisOperator redisOperator=new RedisOperator();*/
    private TokenUtil tokenUtil = new TokenUtil();

    @Autowired
    public BIManageController(MallDealerMapper mallDealerMapper, MallAttrConnectMapper mallAttrConnectMapper, BiMonthProductMapper biMonthProductMapper, SysCustomerMapper sysCustomerMapper, MallProductPlanItemMapper mallProductPlanItemMapper, MallOrderMapper mallOrderMapper, SysDeptMapper sysDeptMapper, JedisPool jedisPool, MallPaymentVoucherMapper mallPaymentVoucherMapper, BiMonthDealerMapper biMonthDealerMapper, MallOrderItemMapper mallOrderItemMapper, SysUserMapper sysUserMapper, BiMonthUserMapper biMonthUserMapper, MallProductMapper mallProductMapper, SysQuestionMapper sysQuestionMapper, SysQuestionAnswerMapper sysQuestionAnswerMapper, SysQuestionAchievementMapper sysQuestionAchievementMapper, DictionMapper dictionMapper) {
        this.mallDealerMapper = mallDealerMapper;
        this.mallAttrConnectMapper = mallAttrConnectMapper;
        this.biMonthProductMapper = biMonthProductMapper;
        this.sysCustomerMapper = sysCustomerMapper;
        this.mallProductPlanItemMapper = mallProductPlanItemMapper;
        this.mallOrderMapper = mallOrderMapper;
        this.sysDeptMapper = sysDeptMapper;
        this.jedisPool = jedisPool;
        this.mallPaymentVoucherMapper = mallPaymentVoucherMapper;
        this.biMonthDealerMapper = biMonthDealerMapper;
        this.mallOrderItemMapper = mallOrderItemMapper;
        this.sysUserMapper = sysUserMapper;
        this.biMonthUserMapper = biMonthUserMapper;
        this.mallProductMapper = mallProductMapper;
        this.sysQuestionMapper = sysQuestionMapper;
        this.sysQuestionAnswerMapper = sysQuestionAnswerMapper;
        this.sysQuestionAchievementMapper = sysQuestionAchievementMapper;
        this.dictionMapper = dictionMapper;
    }

    @GetMapping("getBaiduToken")
    @ApiOperation(value = "更新百度token")
    public ServerResponse<String> getBaiduToken() {
        Map<String, String> map = new HashMap<>();
        map.put("client_secret", "sQ2rcsapqjqFHBveUPB8DvxQnsiugso3");
        map.put("client_id", "DjZavRQ5YUbMRDw7pEDrbHu6");
        map.put("grant_type", "client_credentials");
        String s = HttpClientUtil.doGetWithParam("https://openapi.baidu.com/oauth/2.0/token", map);
        System.out.println(s);
        Gson gson = new Gson();
        Map map2 = new HashMap();
        map2 = gson.fromJson(s, map2.getClass());
        Diction diction = dictionMapper.selectByDictionKey("baiduAccessToken");
        diction.setValue(map2.get("access_token").toString());
        dictionMapper.updateByPrimaryKey(diction);
        return ServerResponse.createBySuccess("更新accessToken成功");
    }

    @LoginRequired
    @GetMapping("getBaiduAccessToken")
    @ApiOperation(value = "获取百度AccessToken")
    public ServerResponse<String> getBaiduAccessToken(String token) {
        Diction diction = dictionMapper.selectByDictionKey("baiduAccessToken");
        return ServerResponse.createBySuccess(diction.getValue());
    }

    @GetMapping("getAchievementList")
    @ApiOperation(value = "获得成绩列表")
    public ServerResponse<List<SysQuestionAchievementVo>> getAchievementList() {
        List<SysQuestionAchievementVo> list = sysQuestionAchievementMapper.selectAllAchievement();
        return ServerResponse.createBySuccess(list);
    }


    @DeleteMapping("deleteQuestion")
    @ApiOperation(value = "删除问题")
    public ServerResponse<String> deleteQuestion(Integer id) {
        sysQuestionMapper.deleteByPrimaryKey(id);
        sysQuestionAnswerMapper.deleteByQUestionId(id);
        return ServerResponse.createBySuccess("删除成功");
    }

    @GetMapping("getQuestionList")
    @ApiOperation(value = "获得问题列表")
    public ServerResponse<List<SysQuestion>> getQuestionList() {
        List<SysQuestion> list = sysQuestionMapper.selectAllQuestion();
        return ServerResponse.createBySuccess(list);
    }

    @GetMapping("getQuestionAnswerList")
    @ApiOperation(value = "获得问题选项列表")
    public ServerResponse<List<SysQuestionAnswer>> getQuestionAnswerList(Integer question_id) {
        List<SysQuestionAnswer> list = sysQuestionAnswerMapper.selectByQuestionId(question_id);
        return ServerResponse.createBySuccess(list);
    }

    @PostMapping("addQuestion")
    @ApiOperation(value = "添加问题")
    public ServerResponse<String> addQuestion(String question, String[] answerList, Integer[] answer) {
        Integer type = 1;
        if (answer.length != 1) {
            type = 2;
        }
        StringBuilder stringAnswer = new StringBuilder();
        List<Integer> list = Arrays.asList(answer);
        SysQuestion sysQuestion = new SysQuestion();
        sysQuestion.setQuestionName(question);
        sysQuestion.setCreateTime(new Date());
        sysQuestion.setType(type);
        sysQuestionMapper.insert(sysQuestion);
        Integer question_id = sysQuestionMapper.selectLastQuestionId();
        for (int i = 0; i < answerList.length; i++) {
            SysQuestionAnswer sysQuestionAnswer = new SysQuestionAnswer();
            sysQuestionAnswer.setAnswer(answerList[i]);
            sysQuestionAnswer.setQuestionId(question_id);
            sysQuestionAnswerMapper.insert(sysQuestionAnswer);
            Integer answer_id = sysQuestionAnswerMapper.selectLastAnswerId();
            if (list.contains(i)) {
                stringAnswer.append(",").append(answer_id);
            }
        }
        System.out.println(stringAnswer);
        stringAnswer = new StringBuilder(stringAnswer.substring(1, stringAnswer.length()));
        System.out.println(stringAnswer);
        SysQuestion sysQuestion1 = sysQuestionMapper.selectByPrimaryKey(question_id);
        sysQuestion1.setAnswerId(stringAnswer.toString());
        sysQuestionMapper.updateByPrimaryKey(sysQuestion1);
        return ServerResponse.createBySuccess("创建成功");
    }

    @PostMapping("updateQuestion")
    @ApiOperation(value = "更新问题")
    public ServerResponse<String> updateQuestion(Integer question_id, String question, String[] answerList, Integer[] answer) {
        Integer type = 1;
        if (answer.length != 1) {
            type = 2;
        }
        StringBuilder stringAnswer = new StringBuilder();
        List<Integer> list = Arrays.asList(answer);
        sysQuestionAnswerMapper.deleteByQUestionId(question_id);
        for (int i = 0; i < answerList.length; i++) {
            SysQuestionAnswer sysQuestionAnswer = new SysQuestionAnswer();
            sysQuestionAnswer.setAnswer(answerList[i]);
            sysQuestionAnswer.setQuestionId(question_id);
            sysQuestionAnswerMapper.insert(sysQuestionAnswer);
            Integer answer_id = sysQuestionAnswerMapper.selectLastAnswerId();
            if (list.contains(i)) {
                stringAnswer.append(",").append(answer_id);
            }
        }
        stringAnswer = new StringBuilder(stringAnswer.substring(1, stringAnswer.length()));
        SysQuestion sysQuestion1 = sysQuestionMapper.selectByPrimaryKey(question_id);
        sysQuestion1.setAnswerId(stringAnswer.toString());
        sysQuestion1.setQuestionName(question);
        sysQuestion1.setType(type);
        sysQuestion1.setUpdateTime(new Date());
        sysQuestionMapper.updateByPrimaryKey(sysQuestion1);
        return ServerResponse.createBySuccess("更新成功");
    }

    @GetMapping("getProductYearList")
    @ApiOperation(value = "获得产品年份信息列表")
    public ServerResponse getProductYearList(String model, Integer year,
                                             String token) throws Exception {
        SysUser user;
        Jedis jedis = jedisPool.getResource();
        user = tokenUtil.tokenToUser(jedis, token).getData();
        jedis.close();

        if (user == null) {
            return ServerResponse.createByErrorMessage("token不存在或失效");
        }

        Integer dept_id = user.getDeptId();
        Integer dealer_id = mallDealerMapper.selectDealerId(dept_id);
        Integer product_id = mallAttrConnectMapper.findProductIdByModel(model);
        List<List<BiMonthProduct>> result = new ArrayList<>();

        for (int i = 1; i <= 12; i++) {
            List<BiMonthProduct> biMonthProduct = biMonthProductMapper.selectMonthData(product_id,
                    dealer_id, i, year);

            if (biMonthProduct.size() == 0) {
                BiMonthProduct biMonthProduct1 = new BiMonthProduct();
                biMonthProduct1.setActual(new BigDecimal(0));
                biMonthProduct1.setDealerId(dealer_id);
                biMonthProduct1.setMonth(i);
                biMonthProduct1.setPlan(new BigDecimal(0));
                biMonthProduct1.setYear(year);
                biMonthProduct1.setProductId(0);
                biMonthProduct1.setType("订货");
                biMonthProduct.add(biMonthProduct1);
            }

            biMonthProduct.get(0).setCreateTime(null);
            result.add(biMonthProduct);
        }

        return ServerResponse.createBySuccess(result);
    }

    @GetMapping("getUserMonthOrderList")
    @ApiOperation(value = "获得用户月份订货列表")
    public ServerResponse getUserMonthOrderList(String token, Integer month,
                                                Integer year) throws Exception {
        SysUser user;
        Jedis jedis = jedisPool.getResource();
        user = tokenUtil.tokenToUser(jedis, token).getData();
        jedis.close();

        if (user == null) {
            return ServerResponse.createByErrorMessage("token不存在或失效");
        }

        Integer dept_id = user.getDeptId();
        List<UserMonthVo> mapList = sysUserMapper.selectByPage(dept_id, month,
                year);

        return ServerResponse.createBySuccess(mapList);
    }

    @AuthRequired
    @GetMapping("getUserMonthPayList")
    @ApiOperation(value = "获得用户月份支付列表")
    public ServerResponse getUserMonthPayList(Integer user_id, Integer month,
                                              Integer year) {
        List<BiMonthUser> biMonthUserList = biMonthUserMapper.selectMonthPayData(user_id,
                month, year);

        if (biMonthUserList.size() == 0) {
            return ServerResponse.createByErrorMessage("本月无数据");
        }

        return ServerResponse.createBySuccess("数据获取成功", biMonthUserList);
    }

    @AuthRequired
    @GetMapping("setDealerMonthData")
    @ApiOperation(value = "设置办事处月份数据")
    public ServerResponse<String> setDealerMonthData() {
        List<MallDealer> mallDealerList = mallDealerMapper.selectAll();

        for (MallDealer aMallDealerList : mallDealerList) {
            Integer dept_id = aMallDealerList.getDeptId();
            Integer dealer_id = mallDealerMapper.selectDealerId(dept_id);
            Calendar cal = Calendar.getInstance();
            int year = cal.get(Calendar.YEAR);
            int month = cal.get(Calendar.MONTH) + 1;
            cal.set(Calendar.YEAR, year);
            cal.set(Calendar.MONTH, month - 1);

            int start = cal.getActualMinimum(Calendar.DAY_OF_MONTH);
            cal.set(Calendar.DAY_OF_MONTH, start);
            cal.set(Calendar.HOUR_OF_DAY, 0);
            cal.set(Calendar.MINUTE, 0);
            cal.set(Calendar.SECOND, 0);
            cal.set(Calendar.MILLISECOND, 0);

            Date startTime = cal.getTime();
            int end = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
            cal.set(Calendar.DAY_OF_MONTH, end);
            cal.set(Calendar.HOUR_OF_DAY, 23);
            cal.set(Calendar.MINUTE, 59);
            cal.set(Calendar.SECOND, 59);
            cal.set(Calendar.MILLISECOND, 999);

            Date endTime = cal.getTime();
            cal.set(Calendar.MONTH, month - 2);

            Date startTime2 = cal.getTime();

            Date endTime2 = cal.getTime();
            List<MallOrder> mallOrderList = mallOrderMapper.selectByMonth(dealer_id,
                    startTime, endTime);
            List<MallPaymentVoucher> mallPaymentVoucherList = mallPaymentVoucherMapper.selectByMonth(dealer_id,
                    startTime, endTime);
            BigDecimal plan = new BigDecimal(0);
            BigDecimal actual = new BigDecimal(0);

            for (MallOrder aMallOrderList : mallOrderList) {
                plan = plan.add(aMallOrderList.getPayment());
            }

            for (MallPaymentVoucher aMallPaymentVoucherList : mallPaymentVoucherList) {
                actual = actual.add(aMallPaymentVoucherList
                        .getFactPayment());
            }

            BiMonthDealer biMonthDealer = new BiMonthDealer();
            biMonthDealer.setActual(actual);
            biMonthDealer.setPlan(plan);
            biMonthDealer.setCreateTime(new Date());
            biMonthDealer.setDeptId(dept_id);
            biMonthDealer.setMonth(month);
            biMonthDealer.setYear(year);
            biMonthDealer.setType("收款");
            biMonthDealerMapper.insert(biMonthDealer);

            BigDecimal plan2 = new BigDecimal(0);
            BigDecimal actual2 = new BigDecimal(0);
            List<MallOrderItem> mallOrderItemList = mallOrderItemMapper.selectByMonth(dealer_id,
                    startTime, endTime);

            for (MallOrderItem aMallOrderItemList : mallOrderItemList) {
                actual2 = actual2.add(aMallOrderItemList
                        .getQuantity());
            }

            List<MallProductPlanItem> mallProductPlanItemList = mallProductPlanItemMapper.selectByMonth(dealer_id,
                    startTime2, endTime2);

            for (MallProductPlanItem aMallProductPlanItemList : mallProductPlanItemList) {
                plan2 = plan2.add(aMallProductPlanItemList
                        .getAmount());
            }

            biMonthDealer.setType("订货");
            biMonthDealer.setActual(actual2);
            biMonthDealer.setPlan(plan2);
            biMonthDealerMapper.insert(biMonthDealer);
        }

        return ServerResponse.createBySuccess("设置成功");
    }

    @AuthRequired
    @GetMapping("setUserMonthData")
    @ApiOperation(value = "设置用户月份数据")
    public ServerResponse<String> setUserMonthData() {
        List<SysUser> sysUserList = sysUserMapper.selectAll();

        for (SysUser aSysUserList : sysUserList) {
            int user_id = aSysUserList.getUserId();
            Calendar cal = Calendar.getInstance();
            int year = cal.get(Calendar.YEAR);
            int month = cal.get(Calendar.MONTH) + 1;
            cal.set(Calendar.YEAR, year);
            cal.set(Calendar.MONTH, month - 1);

            int start = cal.getActualMinimum(Calendar.DAY_OF_MONTH);
            cal.set(Calendar.DAY_OF_MONTH, start);
            cal.set(Calendar.HOUR_OF_DAY, 0);
            cal.set(Calendar.MINUTE, 0);
            cal.set(Calendar.SECOND, 0);
            cal.set(Calendar.MILLISECOND, 0);

            Date startTime = cal.getTime();
            int end = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
            cal.set(Calendar.DAY_OF_MONTH, end);
            cal.set(Calendar.HOUR_OF_DAY, 23);
            cal.set(Calendar.MINUTE, 59);
            cal.set(Calendar.SECOND, 59);
            cal.set(Calendar.MILLISECOND, 999);

            Date endTime = cal.getTime();
            cal.set(Calendar.MONTH, month - 2);

            Date startTime2 = cal.getTime();

            Date endTime2 = cal.getTime();
            BigDecimal plan2 = new BigDecimal(0);
            BigDecimal actual2 = new BigDecimal(0);
            List<MallOrder> mallOrderList = mallOrderMapper.selectByUserIdAndMonth(user_id,
                    startTime, endTime);
            List<MallPaymentVoucher> mallPaymentVoucherList = mallPaymentVoucherMapper.selectByUserIdAndMonth(user_id,
                    startTime, endTime);

            for (MallOrder aMallOrderList : mallOrderList) {
                plan2 = plan2.add(aMallOrderList.getPayment());
            }

            for (MallPaymentVoucher aMallPaymentVoucherList : mallPaymentVoucherList) {
                actual2 = actual2.add(aMallPaymentVoucherList
                        .getFactPayment());
            }

            BigDecimal plan = new BigDecimal(0);
            BigDecimal actual = new BigDecimal(0);
            List<MallOrderItem> mallOrderItemList = mallOrderItemMapper.selectByUserIdAndMonth(user_id,
                    startTime, endTime);

            for (MallOrderItem aMallOrderItemList : mallOrderItemList) {
                actual = actual.add(aMallOrderItemList
                        .getQuantity());
            }

            List<MallProductPlanItem> mallProductPlanItemList = mallProductPlanItemMapper.selectByUserIdAndMonth(user_id,
                    startTime2, endTime2);

            for (MallProductPlanItem aMallProductPlanItemList : mallProductPlanItemList) {
                plan = plan.add(aMallProductPlanItemList
                        .getAmount());
            }

            BiMonthUser biMonthUser = new BiMonthUser();
            biMonthUser.setActual(actual);
            biMonthUser.setPlan(plan);
            biMonthUser.setCreateTime(new Date());
            biMonthUser.setUserId(user_id);
            biMonthUser.setMonth(month);
            biMonthUser.setYear(year);
            biMonthUser.setType("订货");
            biMonthUserMapper.insert(biMonthUser);
            biMonthUser.setType("付款");
            biMonthUser.setActual(actual2);
            biMonthUser.setPlan(plan2);
            biMonthUserMapper.insert(biMonthUser);
        }

        return ServerResponse.createBySuccess("设置成功");
    }

    @GetMapping("getDealerMonthData")
    @ApiOperation(value = "获得办事处月份数据")
    public ServerResponse getDealerMonthData(String token, Integer month,
                                             Integer year) throws Exception {
        System.out.println("token=" + token);

        SysUser user;
        Jedis jedis = jedisPool.getResource();
        System.out.println("aaa=" +
                tokenUtil.tokenToUser(jedis, token).getMsg());
        System.out.println("aaa=" +
                tokenUtil.tokenToUser(jedis, token).getStatus());
        System.out.println("aaa=" +
                tokenUtil.tokenToUser(jedis, token).getData());
        user = tokenUtil.tokenToUser(jedis, token).getData();
        jedis.close();

        if (user == null) {
            return ServerResponse.createByErrorMessage("token不存在或失效");
        }

        Integer dept_id = user.getDeptId();
        List<BiMonthDealer> biMonthDealers = biMonthDealerMapper.selectMonthData(dept_id,
                month, year);

        if (biMonthDealers.size() == 0) {
            return ServerResponse.createByErrorMessage("本月无数据");
        }

        return ServerResponse.createBySuccess("数据获取成功", biMonthDealers);
    }

    @GetMapping("getDealerYearData")
    @ApiOperation(value = "获得办事处年份数据")
    public ServerResponse getDealerYearData(String token, Integer year)
            throws Exception {
        SysUser user;
        Jedis jedis = jedisPool.getResource();
        user = tokenUtil.tokenToUser(jedis, token).getData();
        jedis.close();

        if (user == null) {
            return ServerResponse.createByErrorMessage("token不存在或失效");
        }

        Integer dept_id = user.getDeptId();
        List<List<BiMonthDealer>> result = new ArrayList<>();

        for (int i = 1; i <= 12; i++) {
            List<BiMonthDealer> biMonthDealers = biMonthDealerMapper.selectMonthData(dept_id,
                    i, year);

            if (biMonthDealers.size() == 0) {
                BiMonthDealer biMonthDealer = new BiMonthDealer();
                biMonthDealer.setMonth(i);
                biMonthDealer.setYear(year);
                biMonthDealer.setActual(new BigDecimal(0));
                biMonthDealer.setDeptId(dept_id);
                biMonthDealer.setPlan(new BigDecimal(0));
                biMonthDealer.setType("收款");
                biMonthDealers.add(biMonthDealer);

                BiMonthDealer biMonthDealer2 = new BiMonthDealer();
                biMonthDealer2.setMonth(i);
                biMonthDealer2.setYear(year);
                biMonthDealer2.setActual(new BigDecimal(0));
                biMonthDealer2.setPlan(new BigDecimal(0));
                biMonthDealer2.setType("订货");
                biMonthDealers.add(biMonthDealer2);
            }

            if (biMonthDealers.size() == 1) {
                String type;

                if (biMonthDealers.get(0).getType().equals("收款")) {
                    type = "订货";
                } else {
                    type = "收款";
                }

                BiMonthDealer biMonthDealer = new BiMonthDealer();
                biMonthDealer.setMonth(i);
                biMonthDealer.setYear(year);
                biMonthDealer.setActual(new BigDecimal(0));
                biMonthDealer.setDeptId(dept_id);
                biMonthDealer.setPlan(new BigDecimal(0));
                biMonthDealer.setType(type);
                biMonthDealers.add(biMonthDealer);

                BiMonthDealer biMonthDealer2 = new BiMonthDealer();
                biMonthDealer2.setMonth(i);
                biMonthDealer2.setYear(year);
                biMonthDealer2.setActual(new BigDecimal(0));
                biMonthDealer2.setPlan(new BigDecimal(0));
                biMonthDealer2.setType(type);
                biMonthDealers.add(biMonthDealer2);
            }

            for (BiMonthDealer biMonthDealer : biMonthDealers) {
                biMonthDealer.setId(null);
                biMonthDealer.setCreateTime(null);
                biMonthDealer.setDeptId(null);
            }

            result.add(biMonthDealers);
        }

        return ServerResponse.createBySuccess("数据获取成功", result);
    }

    @AuthRequired
    @GetMapping("setProductMonthData")
    @ApiOperation(value = "设置产品月份数据")
    public ServerResponse<String> setProductMonthData() {
        List<MallProduct> mallProductList = mallProductMapper.selectAll();
        List<MallDealer> mallDealerList = mallDealerMapper.selectAll();
        Calendar cal = Calendar.getInstance();

        //            int year=cal.get(Calendar.YEAR);
        //            int month=cal.get(Calendar.MONTH )+1;
        int year = 2018;
        int month = 8;
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MONTH, month - 1);

        int start = cal.getActualMinimum(Calendar.DAY_OF_MONTH);
        cal.set(Calendar.DAY_OF_MONTH, start);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);

        Date startTime = cal.getTime();
        int end = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        cal.set(Calendar.DAY_OF_MONTH, end);
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);
        cal.set(Calendar.MILLISECOND, 999);

        Date endTime = cal.getTime();
        cal.set(Calendar.MONTH, month - 2);

        Date startTime2 = cal.getTime();

        Date endTime2 = cal.getTime();

        for (MallProduct aMallProductList : mallProductList) {
            int product_id = aMallProductList.getId();

            for (MallDealer aMallDealerList : mallDealerList) {
                int dealer_id = aMallDealerList.getDealerId();
                BigDecimal plan = new BigDecimal(0);
                BigDecimal actual = new BigDecimal(0);
                List<MallOrderItem> mallOrderItemList = mallOrderItemMapper.selectProductMonthData(product_id,
                        dealer_id, startTime, endTime);

                for (MallOrderItem aMallOrderItemList : mallOrderItemList) {
                    actual = actual.add(aMallOrderItemList.getQuantity());
                }

                List<MallProductPlanItem> mallProductPlanItemList = mallProductPlanItemMapper.selectProductMonthData(product_id,
                        dealer_id, startTime2, endTime2);

                for (MallProductPlanItem aMallProductPlanItemList : mallProductPlanItemList) {
                    plan = plan.add(aMallProductPlanItemList.getAmount());
                }

                BiMonthProduct biMonthProduct = new BiMonthProduct();
                biMonthProduct.setActual(actual);
                biMonthProduct.setCreateTime(new Date());
                biMonthProduct.setMonth(month);
                biMonthProduct.setPlan(plan);
                biMonthProduct.setProductId(product_id);
                biMonthProduct.setType("订货");
                biMonthProduct.setYear(year);
                biMonthProduct.setDealerId(dealer_id);
                biMonthProductMapper.insert(biMonthProduct);
            }
        }

        return ServerResponse.createBySuccess("设置成功");
    }
//
//    @AuthRequired
//    @GetMapping("aaa")
//    @ApiOperation(value = "aaa")
//    public void aaa(String path) {
//        StringBuilder laststr= new StringBuilder();
//        File file = new File(path);
//        BufferedReader reader = null;
//        try {
//            FileInputStream in = new FileInputStream(file);
//            reader=new BufferedReader(new InputStreamReader(in,"UTF-8"));
//            String tempString;
//            while ((tempString=reader.readLine())!=null){
//                laststr.append(tempString);
//                System.out.println(tempString);
//            }
//            reader.close();
//        }catch (IOException e){
//            e.printStackTrace();
//        }finally {
//            if (reader!=null){
//                try {
//                    reader.close();
//                }catch (IOException ignored){
//
//                }
//            }
//        }
//    }

    @AuthRequired
    @GetMapping("getBackHomeBack")
    @ApiOperation(value = "获得后台首页数据")
    public ServerResponse<List<Serializable>> getBackHomeBack(Integer dept_id) {
        Integer parent_id = sysDeptMapper.selectParentId(dept_id);
        List<Serializable> list = new ArrayList<>();
        String[] MonthTotalData = new String[20];
        Integer[] UserDailyData = new Integer[20];
        Integer[] CustomerDailyData = new Integer[20];
        Integer[] OrderDailyData = new Integer[20];
        Integer[] VoucherDailyData = new Integer[20];
        String time;
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_MONTH, -19);
        for (int i = 0; i < 20; i++) {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
            time = formatter.format(calendar.getTime());
            System.out.println(time);
            MonthTotalData[i] = time;
            calendar.set(Calendar.HOUR_OF_DAY, 0);
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.SECOND, 0);
            calendar.set(Calendar.MILLISECOND, 0);
            Date startTime = calendar.getTime();
            calendar.set(Calendar.HOUR_OF_DAY, 23);
            calendar.set(Calendar.MINUTE, 59);
            calendar.set(Calendar.SECOND, 59);
            Date endTime = calendar.getTime();
            if (parent_id == 11) {
                //办事处

                //获得每日客户增加量
                Integer CustomerCount = sysCustomerMapper.selectByDeptIdAndTime(dept_id, startTime, endTime);
                CustomerDailyData[i] = CustomerCount;

                //获得每日用户增加量
                Integer UserCount = sysUserMapper.selectByDeptIdAndTime(dept_id, startTime, endTime);
                UserDailyData[i] = UserCount;

                //获得每日订单增加量
                Integer OrderCount = mallOrderMapper.selectByDeptIdAndTime(dept_id, startTime, endTime);
                OrderDailyData[i] = OrderCount;

                //获得每日回款增加量
                Integer VoucherCount = mallPaymentVoucherMapper.selectByDeptIdAndTime(dept_id, startTime, endTime);
                VoucherDailyData[i] = VoucherCount;
            } else {
                //总部

                //获得每日客户增加量
                Integer CustomerCount = sysCustomerMapper.selectByTime(startTime, endTime);
                CustomerDailyData[i] = CustomerCount;

                //获得每日用户增加量
                Integer UserCount = sysUserMapper.selectByTime(startTime, endTime);
                UserDailyData[i] = UserCount;

                //获得每日订单增加量
                Integer OrderCount = mallOrderMapper.selectByTime(startTime, endTime);
                OrderDailyData[i] = OrderCount;

                //获得每日回款增加量
                Integer VoucherCount = mallPaymentVoucherMapper.selectByTime(startTime, endTime);
                VoucherDailyData[i] = VoucherCount;
            }
            calendar.add(Calendar.DAY_OF_MONTH, 1);
        }
        Calendar calendar2 = Calendar.getInstance();
        calendar2.set(Calendar.DAY_OF_MONTH, 1);
        calendar2.set(Calendar.HOUR_OF_DAY, 0);
        calendar2.set(Calendar.MINUTE, 0);
        calendar2.set(Calendar.SECOND, 0);
        calendar2.set(Calendar.MILLISECOND, 0);
        Date startTime2 = calendar2.getTime();
        calendar2.set(Calendar.DAY_OF_MONTH, calendar2.getActualMaximum(Calendar.DAY_OF_MONTH));
        calendar2.set(Calendar.HOUR_OF_DAY, 23);
        calendar2.set(Calendar.MINUTE, 59);
        calendar2.set(Calendar.SECOND, 59);
        calendar2.set(Calendar.MILLISECOND, 999);
        Date endTime2 = calendar2.getTime();
        Integer CustomerCount;
        Integer UserCount;
        Integer OrderCount;
        Integer VoucherCount;
        Integer[] MonthData = new Integer[4];
        if (parent_id == 11) {
            //办事处

            //获得每月客户增加量
            CustomerCount = sysCustomerMapper.selectByDeptIdAndTime(dept_id, startTime2, endTime2);

            //获得每月用户增加量
            UserCount = sysUserMapper.selectByDeptIdAndTime(dept_id, startTime2, endTime2);

            //获得每月订单增加量
            OrderCount = mallOrderMapper.selectByDeptIdAndTime(dept_id, startTime2, endTime2);

            //获得每月回款增加量
            VoucherCount = mallPaymentVoucherMapper.selectByDeptIdAndTime(dept_id, startTime2, endTime2);

            MonthData[0] = CustomerCount;
            MonthData[1] = UserCount;
            MonthData[2] = OrderCount;
            MonthData[3] = VoucherCount;
        } else {
            //总部

            //获得每月客户增加量
            CustomerCount = sysCustomerMapper.selectByTime(startTime2, endTime2);

            //获得每月用户增加量
            UserCount = sysUserMapper.selectByTime(startTime2, endTime2);

            //获得每月订单增加量
            OrderCount = mallOrderMapper.selectByTime(startTime2, endTime2);

            //获得每月回款增加量
            VoucherCount = mallPaymentVoucherMapper.selectByTime(startTime2, endTime2);

            MonthData[0] = CustomerCount;
            MonthData[1] = UserCount;
            MonthData[2] = OrderCount;
            MonthData[3] = VoucherCount;
        }
        list.add(MonthTotalData);
        list.add(CustomerDailyData);
        list.add(UserDailyData);
        list.add(OrderDailyData);
        list.add(VoucherDailyData);
        list.add(MonthData);
        return ServerResponse.createBySuccess(list);
    }
}
